#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import Flask, request, render_template
from mod_weather import getweather
from mod_lnews import getnewslst
from mod_staticeve import getEvent
import json
import pickle

app = Flask(__name__)

@app.route('/_getweather.json')
def weather():
    # Data Format:
    # {'wname': '晴', 'windex': '1', 'temp': '23'}
    p = getweather()
    a = {}
    a['wname'] = p['text']
    a['windex'] = p['code']
    a['temp'] = p['temperature']
    return json.dumps(a)
    
@app.route('/_article')
def article():
    p = pickle.load(open('article.pickle', 'r'));
    return json.dumps(p)
    
@app.route('/form', methods=['GET', 'POST'])
def form():
    if (request.method == 'POST' and request.form['passwd']== 'nowayisway'):
        p = request.form
        pickle.dump(p, open('article.pickle', 'w'))
        return 'Success'
    return render_template('form.html')    

@app.route('/', methods=['GET', 'POST'])
def home():
    elist = getEvent();
    return render_template('index.html', eve=elist)

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)
